package dev.huidocs.docs.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gitee.coadmin.utils.QueryHelpMybatisPlus;
import com.gitee.coadmin.base.PageInfo;
import com.gitee.coadmin.utils.PageUtil;
import lombok.RequiredArgsConstructor;
import dev.huidocs.docs.domain.DocRes;
import dev.huidocs.docs.service.DocResService;
import dev.huidocs.docs.service.dto.DocResDTO;
import dev.huidocs.docs.service.dto.DocResQueryParam;
import dev.huidocs.docs.service.mapper.DocResMapper;
import dev.huidocs.docs.service.converter.DocResConverter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
 * @author jinjin
 * @since 2024-10-11
 */
@Service
@RequiredArgsConstructor
// @CacheConfig(cacheNames = DocResService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DocResServiceImpl implements DocResService {

    // private final RedisUtils redisUtils;
    private final DocResMapper docResMapper;
    private final DocResConverter docResConverter;

    @Override
    public PageInfo<DocResDTO> pageByParam(DocResQueryParam query, Pageable pageable) {
        IPage<DocRes> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<DocRes> page = docResMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return docResConverter.convertPage(page);
    }

    @Override
    public List<DocResDTO> listByParam(DocResQueryParam query){
        return docResConverter.toDto(docResMapper.selectList(QueryHelpMybatisPlus.getPredicate(query, "id", false)));
    }

    @Override
    public long countByParam(DocResQueryParam query) {
        return docResMapper.selectCount(QueryHelpMybatisPlus.getPredicate(query));
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public DocResDTO getById(Long id) {
        if (id == null) {
            return null;
        }
        return docResConverter.toDto(docResMapper.selectById(id));
    }

    @Override
    // @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void insert(DocResDTO res) {
        DocRes entity = docResConverter.toEntity(res);
        docResMapper.insert(entity);
        res.setId(entity.getId());
    }

    @Override
    // @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void updateById(DocResDTO res){
        DocRes entity = docResConverter.toEntity(res);
        docResMapper.updateById(entity);
        // delCaches(res.id);
    }

    @Override
    // @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(Set<Long> ids){
        // delCaches(ids);
        docResMapper.deleteBatchIds(ids);
    }
    
    /*
    private void delCaches(Long id) {
        redisUtils.del(CACHE_KEY + "::id:" + id);
    }

    private void delCaches(Set<Long> ids) {
        for (Long id: ids) {
            delCaches(id);
        }
    }*/
}
